#!/usr/bin/env python3
# Created by han on 2016/6/21
import os,sys,pickle,time
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from conf import db_conf
from module import public
# TEACHER_FILE = db_conf.DB_BASE + "/" + "teacher.db"
TEACHER_FILE = db_conf.SHOOL_DB_BASE + "/" + "teacher.db"

class Teacher:
    """
    创建与老师有关的内容
    缺勤
    挣钱
    """
    def __init__(self, name, passwd, age, sex,interest, speciality):
        """
        构造函数
        :param name:老师姓名
        :param age: 老师年龄
        :param sex: 老师性别
        :param interest:老师兴趣
        :param speciality:老师擅长课程
        :param classtable:老师课程表{li:wuli,wang:yingyu}
        """
        self.name = name
        self.passwd = passwd
        self.age = age
        self.sex = sex
        self.interest = interest
        self.speciality = speciality
        self.classtable = []
        self.asset = 0

    def turant(self):
        """
        老师旷课
        :return:
        """
        self.asset -= 1
        return self.asset

    def gain(self, asset):
        """
        老师上课增加资金
        :return:
        """
        # self.asset += asset
        # print(self.asset)
        teacher_obj_list = pickle.load(open(TEACHER_FILE,"rb"))#读取整个老师对象列表
        for i in teacher_obj_list:#循环列表
            if i.name == self.name:#因为老师姓名是唯一的，所以如果列表中的名字和上课这个名字一样
                msg = "\n\t\t老师原来的钱：{}".format(i.asset)
                public.color_msg(33, msg, 40)
                time.sleep(1)
                i.asset += asset
                msg = "上课后老师的钱：{}".format(i.asset)
                public.color_msg(33, msg, 40)
                time.sleep(1)
                del teacher_obj_list[teacher_obj_list.index(i)]#证明就是这个老师上课，把老的老师对象删除
                teacher_obj_list.append(i)#把+钱的老师对象放里面
                pickle.dump(teacher_obj_list, open(TEACHER_FILE, "wb"))
                msg = "老师加钱完毕"
                public.color_msg(33, msg, 40)
                time.sleep(1)
                return True

    def add_couse(self, course):
        with open(TEACHER_FILE,"rb") as teacherr_file:
            teacher_obj = pickle.load(teacherr_file)
            teacher_obj.classtable.append(course)
        with open(TEACHER_FILE,"wb") as teacherw_file:
            pickle.dump(teacher_obj,teacherw_file)

    def __str__(self):
        return self.name


class Course:
    """
    创建课程有关的内容
    创建课程
    上课
    """
    def __init__(self, name, cost, teacher, exp):
        """
        课程名，课程费用，上课老师
        :param name:课程名
        :param cost: 课程费用
        :param teacher: 上课老师
        "param exp: 经验值
        """
        self.name = name
        self.cost = cost
        self.teacher = teacher
        self.exp = exp

    def attend(self):
        """
        :param course: 课程对象
        上课功能，能给学生增加知识，能给老师增加钱
        :return:返回经验
        """
        self.teacher.gain(self.cost)
        return self.exp

        # cost = course.cost
        # exp = course.exp
        # course.teacher.gain(cost)
